﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Transactions;
using MySql.Data.MySqlClient;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {

            using (TransactionScope tsScope = new TransactionScope(TransactionScopeOption.Required))
            {
                DbConnection conn = GetConnection();
                conn.Open();
                InsertPatient(conn);
                InsertStudy(conn);
                tsScope.Complete();
            }
        }

        private static DbConnection GetConnection()
        {
            DbConnection conn = new MySqlConnection();
            conn.ConnectionString =
                "server=127.0.0.1;User Id=root;Persist Security Info=True;password=root;database=whitecoffee";
            return conn;
        }

        private static void InsertPatient(DbConnection conn)
        {
            //DbTransaction trans = conn.BeginTransaction();
            //try
            //{
            using (TransactionScope tsScope = new TransactionScope(TransactionScopeOption.Required))
            {
            string sql = "INSERT INTO user  (id, NAME, screen_name, PASSWORD, email) VALUES ('1', '1', '1', '1', '1')";
                DbCommand cmd = conn.CreateCommand();
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                tsScope.Complete();
            }
            //trans.Commit();
            //}
            //catch (Exception ex)
            //{
                //trans.Rollback();
            //}
        }

        private static void InsertStudy(DbConnection conn)
        {
            //DbTransaction trans = conn.BeginTransaction();
            //try
            //{
            using (TransactionScope tsScope = new TransactionScope(TransactionScopeOption.Required))
            {
                    string sql = "INSERT INTO user  (id, NAME, screen_name, PASSWORD, email) VALUES ('2', '2', '2', '2', '2')";
                    DbCommand cmd = conn.CreateCommand();
                    cmd.CommandText = sql;
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                    tsScope.Complete();
            }
            //trans.Commit();
            //}
            //catch (Exception ex)
            //{
                //trans.Rollback();
            //}
        }
    }
}
